<?php



namespace Home\Model;

use Think\Model;



/**
*
*/

class CourseModel extends Model

{

	//发布课程

	public function addcourse($data){

		return $this -> add($data);

	}



	/**
	 * 目标用户的课程
	 * @param  [int] $uid      [用户id]
	 * @param  [int] $teaching [教or学]
	 * @param  [int] $id 	   [课程id]
	 * @param  [int] $flag 	   [标记==1,查找通过的课程]
	 * @return [array]         [课程列表]
	 */

	public function userCourses($uid,$teaching,$id='',$flag=''){

		$where['status'] = 1;

		if (!empty($teaching)) {

			//lkl修改非本人只能查看通过的课程

			 if ($uid != session('uid') || $flag == 1) {

				$where['passed'] = 1;

			 }

			$where['teaching'] = $teaching;

		}

		if (!empty($uid)) {

			$where['uid'] = $uid;

		}

		if (!empty($id)) {

			$where['id'] = $id;
			//lkl修改，新增passed字段查询
			return $this -> field('uid,id,title,salary,teaching,content,label,passed') -> where($where) -> find();

		}
		//lkl修改，新增passed字段查询
		return $this -> field('uid,id,title,salary,teaching,content,passed') -> where($where) -> select();

	}

	/**
	 * [findCancel description]查找需要退订的课程
	 * @return [type] [description]
	 * $uid 用户的ID
	 * $cid 课程的ID
	 */

	public function findCancel($uid,$cid){

		$map['m.uid'] = $uid;

		$map['m.cid'] = $cid;

		$map['c.status'] = 1;

		$data = D('Mycourse')->alias('m')

							 ->join('tutor_course as c on m.cid = c.id')

							 ->join('tutor_user_member as u on c.uid = u.id')

							 ->field('c.id,c.title,c.salary,u.nickname,u.logo,u.age,u.sex,u.area_id')

							 ->where($map)

							 ->find();

			  		  return $data;

	}

	/**
	 * [cancelTime description]查找还剩几节课程
	 * @param  [type] $uid [description]用户ID
	 * @param  [type] $cid [description]课程ID
	 * @return [type]      [description]
	 */

	public function cancelTime($uid,$cid){

		 $map['uid'] = $uid;

		 $map['cid'] = $cid;

		 $map['status'] = 1;

		 $times = D('CourseRecord') ->where($map) ->select();

		 return $times;

	}

	/**
	 * [selectCourse description]查询确认的班级信息
	 * @param  [type] $uid [description] 用户ID
	 * @param  [type] $cid [description] 班级ID
	 * @return [type]      [description]
	 */

	public function selectCourse($uid,$cid){

		$map['m.uid'] = $uid;

		$map['m.cid'] = $cid;

		$map['m.type']= 0;

		$map['c.status'] = 1;

		$data = D('Mycourse')->alias('m')

							 ->join('tutor_course as c on m.cid = c.id')

							 ->join('tutor_user_member as u on c.uid = u.id')

							 ->field('c.id,c.title,c.salary,u.nickname,u.logo,u.age,u.sex,u.area_id')

							 ->where($map)

							 ->find();

			$data['class_date'] = D('Mycourse') ->showTime($data['id'],$data['uid']);

			return $data;

	}

	/**
	 * 查找剩余的课时
	 * @param  [type] $uid [description]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */

	public function findNum($uid,$cid){

		$map['cid']		 = $cid;

		$map['uid'] 	 = $uid;

		$map['at_class'] = 0;

		$map['status']   = 1;

		$data = D('CourseRecord') ->where($map)->count();

		return $data;

	}

	/**
	 * 查找班级老师的方法
	 * @param  [type] $uid 用户ID
	 * @param  [type] $cid 班级ID
	 */

	public function showCourseMsg($uid,$cid){

	  	$map['c.id']  = $cid;

	  	$map['c.uid'] = $uid;

	  	//dump($uid);dump($cid);die;

	  	$map['c.status'] = 1;

	    $result = $this

				  ->alias('c')

				  ->join('tutor_mycourse as m on c.id = m.cid')

				  ->join('tutor_user_member as u on u.id = c.uid')

				  ->join('tutor_auth as a on u.id = a.uid')

				  ->field('c.title,c.salary,u.logo,u.id,u.nickname,u.sex,u.age,u.area_id,u.students,a.passed')

				  ->where($map)

				  ->find();

		return $result;

	}

	/**
	 * 查找评论
	 * @param  [type] $cid 课程ID
	 * @param  [type] $uid 用户ID
	 */

	public function showEva($cid,$uid,$order){

		$map['e.course_id'] = $cid;

		$map['e.com_uid']   = $uid;

		$map['e.order']     = $order;

		$data = D('CourseEva')

				->alias('e')

				->join('tutor_user_member as u on e.com_uid = u.id')

				->field('e.*,u.nickname,u.logo')

				->where($map)->find();

	    if(!empty($data['labels'])){

		    $data['labels'] = explode(',',$data['labels']);

		}

		return $data;

	}



	/**
	 * 用户所发布课程的最低价格
	 * @param  [int] $uid      [用户id]
	 * @param  [int] $teaching [教or学]
	 * @return [array]         [数据列表]
	 * @author qlj
	 */

	public function minSalary($uid, $teaching, $label){
		$where['uid'] = $uid;
		$where['teaching'] = $teaching;
          // dump($label);
		if (!empty($label)) {
			$where['label'] = $label;
		}
		if ($teaching == 2) {
			$where['passed'] = 1;
		}
		$data = $this -> field('min(salary) as minsalary') -> where($where) -> find();
		// echo M() ->_Sql();die;
		return $data;
	}

}